package com.hnas.ecard.dao.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.hnas.core.dao.BaseDaoImpl;
import com.hnas.core.dto.DTOPagedList;
import com.hnas.core.util.DateUtil;
import com.hnas.core.util.StringUtil;
import com.hnas.ecard.dao.CommoditMrchDao;
import com.hnas.ecard.mapper.CommoditMapper;
import com.hnas.ecard.mapper.CommoditMrchMapper;
import com.hnas.ecard.orm.Commodit;
import com.hnas.ecard.orm.CommoditMrch;
import com.hnas.ecard.service.dto.DTOCommodit;
import com.hnas.ecard.service.dto.DTOCommoditMrch;
import com.hnas.ecard.service.dto.DTOCommoditMrchSearch;
import com.hnas.ecard.service.search.DTOCommoditSearch;

@Component("commoditMrchDao")
public class CommoditMrchDaoImpl extends BaseDaoImpl<CommoditMrch> implements CommoditMrchDao{

	public CommoditMrchDaoImpl() {
		super(CommoditMrch.class);
	}

	@Override
	public CommoditMrch saveOrUpdate(DTOCommoditMrch dto) throws Exception {
		CommoditMrch temp = null;
		if(null != dto){
			if(StringUtil.isNotEmpty(dto.getId())){
				temp = this.find(dto.getId());
			}
			if(null == temp){
				temp = new CommoditMrch();
				BeanUtils.copyProperties(dto, temp);
				temp.setCreateTime(DateUtil.dateTimeToString(new Date()));
				temp.setUpdateTime(DateUtil.dateTimeToString(new Date()));
				temp.setIsDeleted(false);
				this.persist(temp);
			}else{
				BeanUtils.copyProperties(dto, temp,new String[]{"id", "createTime"});
				temp.setUpdateTime(DateUtil.dateTimeToString(new Date()));
				this.merge(temp);
			}
		}
		return temp;
	}

	@Override
	public List<CommoditMrch> listCommoditMrch() {
		String sql = "select * from tbl_commodit_mrch";
		Query query = this.entityManager.createNativeQuery(sql, CommoditMrch.class);
		return query.getResultList();
	}

	@Override
	public String findMrch(String orderNo) {
		String sql = "select distinct(t2.mrch_id) from tbl_commodit_detail_re t1 left join tbl_commodit t2 on t1.commodit_id = t2.id where t1.order_no = ?1";
		Query query = this.entityManager.createNativeQuery(sql);
		query.setParameter(1, orderNo);
		List<String> resultList = query.getResultList();
		if(resultList != null && resultList.size() > 0) {
			StringBuffer sb = new StringBuffer();
			for(String re : resultList) {
				sb.append(re).append(",");
			}
			return sb.substring(0, sb.length()-1);
		}
		return null;
	}

	@Override
	public DTOPagedList<DTOCommoditMrch> page(DTOCommoditMrchSearch criteria) {
		DTOPagedList<DTOCommoditMrch> pageList = new DTOPagedList<DTOCommoditMrch>();
		List<DTOCommoditMrch> list = new ArrayList<DTOCommoditMrch>();
		int pageNo = criteria.getPageNo();
		int pageSize = criteria.getPageSize();
		List<Object> paras = new ArrayList<Object>();
		StringBuffer baseSql = new StringBuffer();
		baseSql.append(" from tbl_commodit_mrch where 1 = 1 ");
		int j = 1;
		/*if(StringUtil.isNotEmpty(criteria.getOrderNo())){
			baseSql.append(" and t1.order_no = ?"+j++);
			paras.add(criteria.getOrderNo().trim());
		}*/
		if(StringUtil.isNotEmpty(criteria.getMrchId())){
			baseSql.append(" and mrch_id = ?"+j++);
			paras.add(criteria.getMrchId().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getMrchName())){
			baseSql.append(" and mrch_name = ?"+j++);
			paras.add(criteria.getMrchName().trim());
		}
		baseSql.append(" order by create_time desc");
		String countSql = "select count(1) "+baseSql.toString();
		String listSql = "select * "+baseSql.toString();
		Query countQuery = this.entityManager.createNativeQuery(countSql);
		Query listQuery = this.entityManager.createNativeQuery(listSql,CommoditMrch.class);
		for(int i= 1; i <= paras.size(); i++) {
			listQuery.setParameter(i, paras.get(i-1));
			countQuery.setParameter(i, paras.get(i-1));
		}
		listQuery.setFirstResult((pageNo-1)*pageSize);
		listQuery.setMaxResults(pageSize);
		List<CommoditMrch> resultList = listQuery.getResultList();
		int count = ((BigInteger)countQuery.getSingleResult()).intValue();
		pageList.setPageNo(pageNo);
		pageList.setPageSize(pageSize);
		pageList.setTotalCount(count);
		if(resultList != null && resultList.size() > 0) {
			list = CommoditMrchMapper.getInstance().mapToValue(resultList);
		}
		pageList.setList(list);
		return pageList;
	}

	@Override
	public boolean deleteById(String id) {
		if(StringUtil.isEmpty(id)) return false;
		String sql = "delete from tbl_commodit_mrch where id = ?1";
		Query query = this.entityManager.createNativeQuery(sql);
		query.setParameter(1, id);
		return query.executeUpdate()==1;
	}

	@Override
	public DTOPagedList<DTOCommodit> pageCommodit(DTOCommoditSearch criteria) {
		DTOPagedList<DTOCommodit> pageList = new DTOPagedList<DTOCommodit>();
		List<DTOCommodit> list = new ArrayList<DTOCommodit>();
		int pageNo = criteria.getPageNo();
		int pageSize = criteria.getPageSize();
		List<Object> paras = new ArrayList<Object>();
		StringBuffer baseSql = new StringBuffer();
		baseSql.append(" from tbl_commodit where 1 = 1 ");
		int j = 1;
		/*if(StringUtil.isNotEmpty(criteria.getOrderNo())){
			baseSql.append(" and t1.order_no = ?"+j++);
			paras.add(criteria.getOrderNo().trim());
		}*/
		if(StringUtil.isNotEmpty(criteria.getMrchId())){
			baseSql.append(" and mrch_id = ?"+j++);
			paras.add(criteria.getMrchId().trim());
		}
		baseSql.append(" order by create_time desc");
		String countSql = "select count(1) "+baseSql.toString();
		String listSql = "select * "+baseSql.toString();
		Query countQuery = this.entityManager.createNativeQuery(countSql);
		Query listQuery = this.entityManager.createNativeQuery(listSql,Commodit.class);
		for(int i= 1; i <= paras.size(); i++) {
			listQuery.setParameter(i, paras.get(i-1));
			countQuery.setParameter(i, paras.get(i-1));
		}
		listQuery.setFirstResult((pageNo-1)*pageSize);
		listQuery.setMaxResults(pageSize);
		List<Commodit> resultList = listQuery.getResultList();
		int count = ((BigInteger)countQuery.getSingleResult()).intValue();
		pageList.setPageNo(pageNo);
		pageList.setPageSize(pageSize);
		pageList.setTotalCount(count);
		if(resultList != null && resultList.size() > 0) {
			list = CommoditMapper.getInstance().mapToValue(resultList);
		}
		pageList.setList(list);
		return pageList;
	}

}
